package sk.patt.eshop.data.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.transaction.annotation.Transactional;

import sk.patt.eshop.data.dao.interfaces.SubcategoryDao;
import sk.patt.eshop.data.dataobjects.Subcategory;

public class SubcategoryDaoJPAImp extends AbstractDaoJPAImpl<Subcategory> implements SubcategoryDao {

	public SubcategoryDaoJPAImp() {
		super(Subcategory.class);
	}

	@Transactional
	public List<Subcategory> findAll() {
		return getJpaTemplate().execute(new JpaCallback<List<Subcategory>>() {
			public List<Subcategory> doInJpa(EntityManager em) throws PersistenceException {
				TypedQuery<Subcategory> query = em.createQuery("select s from Subcategory s", Subcategory.class);
				return query.getResultList();
			}
		});
	}

	@Transactional
	public int countAll() {
		return getJpaTemplate().execute(new JpaCallback<Integer>() {

			public Integer doInJpa(EntityManager em) throws PersistenceException {
				TypedQuery<Long> query = em.createQuery("select count (s) from Subcategory s", Long.class);
				return (query.getSingleResult()).intValue();
			}
		});
	}

}
